package com.hosa.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hosa.constants.SystemConstants;
import com.hosa.domain.ResponseResult;
import com.hosa.domain.entity.Article;
import com.hosa.domain.entity.Category;
import com.hosa.domain.vo.CategoryVo;
import com.hosa.mapper.CategoryMapper;
import com.hosa.service.ArticleService;
import com.hosa.service.CategoryService;
import com.hosa.utils.BeanCopyUtils;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2024-04-08 18:49:59
 */
@Service("categoryService")

public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private ArticleService articleService;
    @Override
    public ResponseResult getCategoryList() {
        //查询已发布文章表
        LambdaQueryWrapper<Article> articlewrapper=new LambdaQueryWrapper<>();
        articlewrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(articlewrapper);
        //获取文章分类id、去重
        Set<Long> categoryIds = articleList.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());
        //查询分类表
        List<Category> categories = listByIds(categoryIds);

        categories.stream().filter(category ->SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }
}
